//
//  MainViewController.m
//  blockDemo
//
//  Created by wxc on 2019/1/8.
//  Copyright © 2019 wxc. All rights reserved.
//

#import "MainViewController.h"
#import <Photos/Photos.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>


@interface MainViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation MainViewController

- (void)pushPhoto {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    
    imagePicker.allowsEditing = YES;
    
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    imagePicker.delegate = self;
    
    [self presentViewController:imagePicker animated:YES completion:^{
        
    }];
}

static void choosePhoto(MainViewController *object) {
    [object requireDevicePremissionWithComplete:^(BOOL success) {
        
        if (success) {
            
            [object presentViewController:({
                
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"想要看看照片么" message:@"点击✅进入" preferredStyle:(UIAlertControllerStyleAlert)];
                [alert addAction:[UIAlertAction actionWithTitle:@"✅" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                    [object pushPhoto];
                }]];
                [alert addAction:[UIAlertAction actionWithTitle:@"❌" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                alert;
                
            }) animated:YES completion:^{
                
            }];
        }
    }];
}

static void blockFunction(MainViewController *object) {
    [object block1:^(BOOL success) {
        
        if (success) {
            
        }
        else {
            NSLog(@"NO");
        }
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.titleView = ({
        
        UILabel * titleLabel = [UILabel new];
        titleLabel.text = @"主页";
        titleLabel;
        
    });
    
    choosePhoto(self);
    
    [self.view addSubview:({
        
        UIButton * btn = [UIButton new];
        btn.frame = CGRectMake(100, 100, 50, 50);
        btn.backgroundColor = [UIColor redColor];
        [btn addTarget:self action:@selector(chooseBtnTarget:) forControlEvents:(UIControlEventTouchUpInside)];
        btn;
    
    })];
 
    int (^b1)(int,int) = ^(int a,int b){
        return a+b;
    };
    
    //void (^myblock)       ()       =    ^()       {   };
    //类型（^block的名称）（参数类型）    =  (参数类型)   {代码内容};
    //使用：类似于函数调用
    
    NSLog(@"%d",b1(3,4));
    
    
    
    // 指针
    int (* p)(int , int) = sum;
    
    int z = p(1,b1(3,4));
    
    NSLog(@"%d",z);
  
    blockFunction(self);
    
}

int sum (int a ,int b)
{
    return a + b;
}

- (void)requireDevicePremissionWithComplete:(void (^)(BOOL success))complete{
    
    NSString *mediaType = AVMediaTypeVideo;
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    
    switch (authStatus) {
        case AVAuthorizationStatusRestricted:
            complete(NO);
            break;
            
        case AVAuthorizationStatusDenied:
            complete(NO);
            break;
            
        default:
            complete(YES);
            break;
    }
}

- (void)chooseBtnTarget:(UIButton *)sender{
 
    [self requireDevicePremissionWithComplete:^(BOOL success) {
        
        if (success) {
            
            [self presentViewController:({
                
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"想要看看照片么" message:@"点击✅进入" preferredStyle:(UIAlertControllerStyleAlert)];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"✅" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                    [self pushPhoto];
                }]];
                [alert addAction:[UIAlertAction actionWithTitle:@"❌" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                }]];
                
                alert;
                
            }) animated:YES completion:^{
                
            }];
        }
    }];
    
}

- (void)block1:(void (^)(BOOL success))complete{
  
    complete(NO);
    
}
    
    


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
